<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:alg="http://tecnologie-web.studenti.math.unipd.it/tecweb/~ccardin/"
    exclude-result-prefixes="alg">
        
<xsl:output method='xml' version='1.0' encoding='UTF-8' indent='yes' omit-xml-declaration='yes'/>

    <!--Variabile per selezionare un determinato algoritmo-->
    
    <xsl:variable name="idAlg" select="_IDALG_"/>
     <!--xsl:variable name="idAlg" select="1"/-->
    
    <xsl:template match="/">
	<div id="searchbar">
		    <form method="post" action="forumsearch.cgi">
    			<div><input type="text" name="query" size="27"></input>
			<input type="submit" value="Search"></input></div>
		    </form>
	
	</div>
        <xsl:apply-templates select="alg:algorithms/alg:category/alg:subCategory/alg:algorithm[@id=$idAlg]"/>
    </xsl:template>
    
    <xsl:template match="alg:algorithm">
        <div id="content-title">
            <h2><xsl:value-of select="alg:description"/></h2>
            <h3>Author: <xsl:value-of select="alg:author"/></h3>
        </div>
        <div id="content-algorithm">
            <h4>Algorithm Source:</h4>
            <div class="code_notranslate">
                <pre><xsl:value-of select="alg:source"/></pre>
            </div>
            <h4>Algorithm Informations:</h4>
            <div id="content-notes">
                <p>Insert Date: <xsl:value-of select="alg:insertDate"/></p>
                <h4>Rating:
		<xsl:variable name="numVotes" select="count(./alg:ratings//alg:vote)" />
		    <xsl:choose>
			<xsl:when test="$numVotes=0">
			    no rating
			</xsl:when>
			<xsl:otherwise>
			    <xsl:variable name="rate" select="round(sum(./alg:ratings//alg:vote/alg:value) div $numVotes)" />
			    <img src="../public_html/images/star{$rate}.gif" alt="rate {$rate} of 5"/>
			    (<xsl:value-of select="$numVotes"/> votes)
			</xsl:otherwise>
		    </xsl:choose>
		
		
		</h4>	
		
      
		<p><xsl:value-of select="alg:notes"/></p>
            </div>
        </div>
        <div id="content-comments">
            <xsl:if test="count(alg:comments/alg:comment)=0">
                <h4>There are no comments for this algorithm</h4>
            </xsl:if>
            <xsl:if test="count(alg:comments/alg:comment)!=0">
                <h4>Comments:</h4>
                <xsl:for-each select="alg:comments/alg:comment">
                    <div class="content-comment">
                        <div class="comment-info">
			    <h5>Sender: <xsl:value-of select="alg:sender"/><br/>
			    
				<xsl:variable name="date" select="substring-before(alg:postDate, 'T')" />
				<xsl:variable name="year" select="substring-before($date, '-')" />
				<xsl:variable name="month" select="substring-before(substring-after($date, '-'), '-')" />
				<xsl:variable name="day" select="substring-after(substring-after($date, '-'), '-')" />
				<xsl:variable name="time" select="substring-after(alg:postDate, 'T')" />
				
				<xsl:value-of select="concat('at ', $time)" />
				<xsl:value-of select="concat(' - ', $day, '/', $month, '/', $year)" />
			    </h5>
			</div>
                        Message:<p class="message"><xsl:value-of select="alg:text"/></p>
                    </div>
				<p class="to-top">
					 <a href="#content">Return to top</a><br/>
					 <a class="to-menu" href="#nav">Return to menu</a>
				</p>
                </xsl:for-each>
            </xsl:if>
        </div>
        

    </xsl:template>
    
    
</xsl:stylesheet>
